home *** CD-ROM | disk | FTP | other *** search
/ HAKERIS 11 / HAKERIS 11.ISO / linux / system / LinuxConsole 0.4 / linuxconsole0.4install-en.iso / guile0.4.lcm / share / guile / slib / format.scm < prev    next >
Encoding:
Text File  |  2004-01-06  |  55.4 KB  |  1,676 lines

  1. ;;; "format.scm" Common LISP text output formatter for SLIB
  2. ; Written 1992-1994 by Dirk Lutzebaeck (lutzeb@cs.tu-berlin.de)
  3. ;
  4. ; This code is in the public domain.
  5.  
  6. ; Authors of the original version (< 1.4) were Ken Dickey and Aubrey Jaffer.
  7. ; Please send error reports to the email address above.
  8. ; For documentation see slib.texi and format.doc.
  9. ; For testing load formatst.scm.
  10. ;
  11. ; Version 3.0
  12.  
  13. (provide 'format)
  14. (require 'string-case)
  15. (require 'string-port)
  16. (require 'rev4-optional-procedures)
  17.  
  18. ;;; Configuration ------------------------------------------------------------
  19.  
  20. (define format:symbol-case-conv #f)
  21. ;; Symbols are converted by symbol->string so the case of the printed
  22. ;; symbols is implementation dependent. format:symbol-case-conv is a
  23. ;; one arg closure which is either #f (no conversion), string-upcase!,
  24. ;; string-downcase! or string-capitalize!.
  25.  
  26. (define format:iobj-case-conv #f)
  27. ;; As format:symbol-case-conv but applies for the representation of
  28. ;; implementation internal objects.
  29.  
  30. (define format:expch #\E)
  31. ;; The character prefixing the exponent value in ~e printing.
  32.  
  33. (define format:floats (provided? 'inexact))
  34. ;; Detects if the scheme system implements flonums (see at eof).
  35.  
  36. (define format:complex-numbers (provided? 'complex))
  37. ;; Detects if the scheme system implements complex numbers.
  38.  
  39. (define format:radix-pref (char=? #\# (string-ref (number->string 8 8) 0)))
  40. ;; Detects if number->string adds a radix prefix.
  41.  
  42. (define format:ascii-non-printable-charnames
  43.   '#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel"
  44.      "bs"  "ht"  "nl"  "vt"  "np"  "cr"  "so"  "si"
  45.      "dle" "dc1" "dc2" "dc3" "dc4" "nak" "syn" "etb"
  46.      "can" "em"  "sub" "esc" "fs"  "gs"  "rs"  "us" "space"))
  47.  
  48. ;;; End of configuration ----------------------------------------------------
  49.  
  50. (define format:version "3.0")
  51. (define format:port #f)            ; curr. format output port
  52. (define format:output-col 0)        ; curr. format output tty column
  53. (define format:flush-output #f)        ; flush output at end of formatting
  54. (define format:case-conversion #f)
  55. (define format:error-continuation #f)
  56. (define format:args #f)
  57. (define format:pos 0)            ; curr. format string parsing position
  58. (define format:arg-pos 0)        ; curr. format argument position
  59.                     ; this is global for error presentation
  60.  
  61. ; format string and char output routines on format:port
  62.  
  63. (define (format:out-str str)
  64.   (if format:case-conversion
  65.       (display (format:case-conversion str) format:port)
  66.       (display str format:port))
  67.   (set! format:output-col
  68.     (+ format:output-col (string-length str))))
  69.  
  70. (define (format:out-char ch)
  71.   (if format:case-conversion
  72.       (display (format:case-conversion (string ch)) format:port)
  73.       (write-char ch format:port))
  74.   (set! format:output-col
  75.     (if (char=? ch #\newline)
  76.         0
  77.         (+ format:output-col 1))))
  78.  
  79. ;(define (format:out-substr str i n)  ; this allocates a new string
  80. ;  (display (substring str i n) format:port)
  81. ;  (set! format:output-col (+ format:output-col n)))
  82.  
  83. (define (format:out-substr str i n)
  84.   (do ((k i (+ k 1)))
  85.       ((= k n))
  86.     (write-char (string-ref str k) format:port))
  87.   (set! format:output-col (+ format:output-col n)))
  88.  
  89. ;(define (format:out-fill n ch)       ; this allocates a new string
  90. ;  (format:out-str (make-string n ch)))
  91.  
  92. (define (format:out-fill n ch)
  93.   (do ((i 0 (+ i 1)))
  94.       ((= i n))
  95.     (write-char ch format:port))
  96.   (set! format:output-col (+ format:output-col n)))
  97.  
  98. ; format's user error handler
  99.  
  100. (define (format:error . args)        ; never returns!
  101.   (let ((error-continuation format:error-continuation)
  102.     (format-args format:args)
  103.     (port (current-error-port)))
  104.     (set! format:error format:intern-error)
  105.     (if (and (>= (length format:args) 2)
  106.          (string? (cadr format:args)))
  107.     (let ((format-string (cadr format-args)))
  108.       (if (not (zero? format:arg-pos))
  109.           (set! format:arg-pos (- format:arg-pos 1)))
  110.       (format port "~%FORMAT: error with call: (format ~a \"~a<===~a\" ~
  111.                                   ~{~a ~}===>~{~a ~})~%        "
  112.           (car format:args)
  113.           (substring format-string 0 format:pos)
  114.           (substring format-string format:pos
  115.                  (string-length format-string))
  116.           (format:list-head (cddr format:args) format:arg-pos)
  117.           (list-tail (cddr format:args) format:arg-pos)))
  118.     (format port
  119.         "~%FORMAT: error with call: (format~{ ~a~})~%        "
  120.         format:args))
  121.     (apply format port args)
  122.     (newline port)
  123.     (set! format:error format:error-save)
  124.     (set! format:error-continuation error-continuation)
  125.     (format:abort)
  126.     (format:intern-error "format:abort does not jump to toplevel!")))
  127.  
  128. (define format:error-save format:error)
  129.  
  130. (define (format:intern-error . args)   ;if something goes wrong in format:error
  131.   (display "FORMAT: INTERNAL ERROR IN FORMAT:ERROR!") (newline)
  132.   (display "        format args: ") (write format:args) (newline)
  133.   (display "        error args:  ") (write args) (newline)
  134.   (set! format:error format:error-save)
  135.   (format:abort))
  136.  
  137. (define (format:format . args)        ; the formatter entry
  138.   (set! format:args args)
  139.   (set! format:arg-pos 0)
  140.   (set! format:pos 0)
  141.   (if (< (length args) 1)
  142.       (format:error "not enough arguments"))
  143.  
  144.   ;; If the first argument is a string, then that's the format string.
  145.   ;; (Scheme->C)
  146.   ;; In this case, put the argument list in canonical form.
  147.   (let ((args (if (string? (car args))
  148.           (cons #f args)
  149.           args)))
  150.     ;; Use this canonicalized version when reporting errors.
  151.     (set! format:args args)
  152.  
  153.     (let ((destination (car args))
  154.       (arglist (cdr args)))
  155.       (cond
  156.        ((or (and (boolean? destination)    ; port output
  157.          destination)
  158.         (output-port? destination)
  159.         (number? destination))
  160.     (format:out (cond
  161.              ((boolean? destination) (current-output-port))
  162.              ((output-port? destination) destination)
  163.              ((number? destination) (current-error-port)))
  164.             (car arglist) (cdr arglist)))
  165.        ((and (boolean? destination)    ; string output
  166.          (not destination))
  167.     (call-with-output-string
  168.      (lambda (port) (format:out port (car arglist) (cdr arglist)))))
  169.        (else
  170.     (format:error "illegal destination `~a'" destination))))))
  171.  
  172. (define (format:out port fmt args)    ; the output handler for a port
  173.   (set! format:port port)        ; global port for output routines
  174.   (set! format:case-conversion #f)    ; modifier case conversion procedure
  175.   (set! format:flush-output #f)        ; ~! reset
  176.   (let ((arg-pos (format:format-work fmt args))
  177.     (arg-len (length args)))
  178.     (cond
  179.      ((< arg-pos arg-len)
  180.       (set! format:arg-pos (+ arg-pos 1))
  181.       (set! format:pos (string-length fmt))
  182.       (format:error "~a superfluous argument~:p" (- arg-len arg-pos)))
  183.      ((> arg-pos arg-len)
  184.       (set! format:arg-pos (+ arg-len 1))
  185.       (display format:arg-pos)
  186.       (format:error "~a missing argument~:p" (- arg-pos arg-len)))
  187.      (else
  188.       (if format:flush-output (force-output port))
  189.       #t))))
  190.  
  191. (define format:parameter-characters
  192.   '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+ #\v #\# #\'))
  193.  
  194. (define (format:format-work format-string arglist) ; does the formatting work
  195.   (letrec
  196.       ((format-string-len (string-length format-string))
  197.        (arg-pos 0)            ; argument position in arglist
  198.        (arg-len (length arglist))    ; number of arguments
  199.        (modifier #f)            ; 'colon | 'at | 'colon-at | #f
  200.        (params '())            ; directive parameter list
  201.        (param-value-found #f)        ; a directive parameter value found
  202.        (conditional-nest 0)        ; conditional nesting level
  203.        (clause-pos 0)            ; last cond. clause beginning char pos
  204.        (clause-default #f)        ; conditional default clause string
  205.        (clauses '())            ; conditional clause string list
  206.        (conditional-type #f)        ; reflects the contional modifiers
  207.        (conditional-arg #f)        ; argument to apply the conditional
  208.        (iteration-nest 0)        ; iteration nesting level
  209.        (iteration-pos 0)        ; iteration string beginning char pos
  210.        (iteration-type #f)        ; reflects the iteration modifiers
  211.        (max-iterations #f)        ; maximum number of iterations
  212.        (recursive-pos-save format:pos)
  213.  
  214.        (next-char            ; gets the next char from format-string
  215.     (lambda ()
  216.       (let ((ch (peek-next-char)))
  217.         (set! format:pos (+ 1 format:pos))
  218.         ch)))
  219.  
  220.        (peek-next-char
  221.     (lambda ()
  222.       (if (>= format:pos format-string-len)
  223.           (format:error "illegal format string")
  224.           (string-ref format-string format:pos))))
  225.  
  226.        (one-positive-integer?
  227.     (lambda (params)
  228.       (cond
  229.        ((null? params) #f)
  230.        ((and (integer? (car params))
  231.          (>= (car params) 0)
  232.          (= (length params) 1)) #t)
  233.        (else (format:error "one positive integer parameter expected")))))
  234.  
  235.        (next-arg
  236.     (lambda ()
  237.       (if (>= arg-pos arg-len)
  238.           (begin
  239.         (set! format:arg-pos (+ arg-len 1))
  240.         (format:error "missing argument(s)")))
  241.       (add-arg-pos 1)
  242.       (list-ref arglist (- arg-pos 1))))
  243.  
  244.        (prev-arg
  245.     (lambda ()
  246.       (add-arg-pos -1)
  247.       (if (negative? arg-pos)
  248.           (format:error "missing backward argument(s)"))
  249.       (list-ref arglist arg-pos)))
  250.  
  251.        (rest-args
  252.     (lambda ()
  253.       (let loop ((l arglist) (k arg-pos)) ; list-tail definition
  254.         (if (= k 0) l (loop (cdr l) (- k 1))))))
  255.  
  256.        (add-arg-pos
  257.     (lambda (n)
  258.       (set! arg-pos (+ n arg-pos))
  259.       (set! format:arg-pos arg-pos)))
  260.  
  261.        (anychar-dispatch        ; dispatches the format-string
  262.     (lambda ()
  263.       (if (>= format:pos format-string-len)
  264.           arg-pos            ; used for ~? continuance
  265.           (let ((char (next-char)))
  266.         (cond
  267.          ((char=? char #\~)
  268.           (set! modifier #f)
  269.           (set! params '())
  270.           (set! param-value-found #f)
  271.           (tilde-dispatch))
  272.          (else
  273.           (if (and (zero? conditional-nest)
  274.                (zero? iteration-nest))
  275.               (format:out-char char))
  276.           (anychar-dispatch)))))))
  277.  
  278.        (tilde-dispatch
  279.     (lambda ()
  280.       (cond
  281.        ((>= format:pos format-string-len)
  282.         (format:out-str "~")    ; tilde at end of string is just output
  283.         arg-pos)            ; used for ~? continuance
  284.        ((and (or (zero? conditional-nest)
  285.              (memv (peek-next-char) ; find conditional directives
  286.                (append '(#\[ #\] #\; #\: #\@ #\^)
  287.                    format:parameter-characters)))
  288.          (or (zero? iteration-nest)
  289.              (memv (peek-next-char) ; find iteration directives
  290.                (append '(#\{ #\} #\: #\@ #\^)
  291.                    format:parameter-characters))))
  292.         (case (char-upcase (next-char))
  293.  
  294.           ;; format directives
  295.  
  296.           ((#\A)            ; Any -- for humans
  297.            (set! format:read-proof (memq modifier '(colon colon-at)))
  298.            (format:out-obj-padded (memq modifier '(at colon-at))
  299.                       (next-arg) #f params)
  300.            (anychar-dispatch))
  301.           ((#\S)            ; Slashified -- for parsers
  302.            (set! format:read-proof (memq modifier '(colon colon-at)))
  303.            (format:out-obj-padded (memq modifier '(at colon-at))
  304.                       (next-arg) #t params)
  305.            (anychar-dispatch))
  306.           ((#\D)            ; Decimal
  307.            (format:out-num-padded modifier (next-arg) params 10)
  308.            (anychar-dispatch))
  309.           ((#\X)            ; Hexadecimal
  310.            (format:out-num-padded modifier (next-arg) params 16)
  311.            (anychar-dispatch))
  312.           ((#\O)            ; Octal
  313.            (format:out-num-padded modifier (next-arg) params 8)
  314.            (anychar-dispatch))
  315.           ((#\B)            ; Binary
  316.            (format:out-num-padded modifier (next-arg) params 2)
  317.            (anychar-dispatch))
  318.           ((#\R)
  319.            (if (null? params)
  320.            (format:out-obj-padded ; Roman, cardinal, ordinal numerals
  321.             #f
  322.             ((case modifier
  323.                ((at) format:num->roman)
  324.                ((colon-at) format:num->old-roman)
  325.                ((colon) format:num->ordinal)
  326.                (else format:num->cardinal))
  327.              (next-arg))
  328.             #f params)
  329.            (format:out-num-padded ; any Radix
  330.             modifier (next-arg) (cdr params) (car params)))
  331.            (anychar-dispatch))
  332.           ((#\F)            ; Fixed-format floating-point
  333.            (if format:floats
  334.            (format:out-fixed modifier (next-arg) params)
  335.            (format:out-str (number->string (next-arg))))
  336.            (anychar-dispatch))
  337.           ((#\E)            ; Exponential floating-point
  338.            (if format:floats
  339.            (format:out-expon modifier (next-arg) params)
  340.            (format:out-str (number->string (next-arg))))
  341.            (anychar-dispatch))
  342.           ((#\G)            ; General floating-point
  343.            (if format:floats
  344.            (format:out-general modifier (next-arg) params)
  345.            (format:out-str (number->string (next-arg))))
  346.            (anychar-dispatch))
  347.           ((#\$)            ; Dollars floating-point
  348.            (if format:floats
  349.            (format:out-dollar modifier (next-arg) params)
  350.            (format:out-str (number->string (next-arg))))
  351.            (anychar-dispatch))
  352.           ((#\I)            ; Complex numbers
  353.            (if (not format:complex-numbers)
  354.            (format:error
  355.             "complex numbers not supported by this scheme system"))
  356.            (let ((z (next-arg)))
  357.          (if (not (complex? z))
  358.              (format:error "argument not a complex number"))
  359.          (format:out-fixed modifier (real-part z) params)
  360.          (format:out-fixed 'at (imag-part z) params)
  361.          (format:out-char #\i))
  362.            (anychar-dispatch))
  363.           ((#\C)            ; Character
  364.            (let ((ch (if (one-positive-integer? params)
  365.                  (integer->char (car params))
  366.                  (next-arg))))
  367.          (if (not (char? ch)) (format:error "~~c expects a character"))
  368.          (case modifier
  369.            ((at)
  370.             (format:out-str (format:char->str ch)))
  371.            ((colon)
  372.             (let ((c (char->integer ch)))
  373.               (if (< c 0)
  374.               (set! c (+ c 256))) ; compensate complement impl.
  375.               (cond
  376.                ((< c #x20)    ; assumes that control chars are < #x20
  377.             (format:out-char #\^)
  378.             (format:out-char
  379.              (integer->char (+ c #x40))))
  380.                ((>= c #x7f)
  381.             (format:out-str "#\\")
  382.             (format:out-str
  383.              (if format:radix-pref
  384.                  (let ((s (number->string c 8)))
  385.                    (substring s 2 (string-length s)))
  386.                  (number->string c 8))))
  387.                (else
  388.             (format:out-char ch)))))
  389.            (else (format:out-char ch))))
  390.            (anychar-dispatch))
  391.           ((#\P)            ; Plural
  392.            (if (memq modifier '(colon colon-at))
  393.            (prev-arg))
  394.            (let ((arg (next-arg)))
  395.          (if (not (number? arg))
  396.              (format:error "~~p expects a number argument"))
  397.          (if (= arg 1)
  398.              (if (memq modifier '(at colon-at))
  399.              (format:out-char #\y))
  400.              (if (memq modifier '(at colon-at))
  401.              (format:out-str "ies")
  402.              (format:out-char #\s))))
  403.            (anychar-dispatch))
  404.           ((#\~)            ; Tilde
  405.            (if (one-positive-integer? params)
  406.            (format:out-fill (car params) #\~)
  407.            (format:out-char #\~))
  408.            (anychar-dispatch))
  409.           ((#\%)            ; Newline
  410.            (if (one-positive-integer? params)
  411.            (format:out-fill (car params) #\newline)
  412.            (format:out-char #\newline))
  413.            (set! format:output-col 0)
  414.            (anychar-dispatch))
  415.           ((#\&)            ; Fresh line
  416.            (if (one-positive-integer? params)
  417.            (begin
  418.              (if (> (car params) 0)
  419.              (format:out-fill (- (car params)
  420.                          (if (> format:output-col 0) 0 1))
  421.                       #\newline))
  422.              (set! format:output-col 0))
  423.            (if (> format:output-col 0)
  424.                (format:out-char #\newline)))
  425.            (anychar-dispatch))
  426.           ((#\_)            ; Space character
  427.            (if (one-positive-integer? params)
  428.            (format:out-fill (car params) #\space)
  429.            (format:out-char #\space))
  430.            (anychar-dispatch))
  431.           ((#\/)            ; Tabulator character
  432.            (if (one-positive-integer? params)
  433.            (format:out-fill (car params) slib:tab)
  434.            (format:out-char slib:tab))
  435.            (anychar-dispatch))
  436.           ((#\|)            ; Page seperator
  437.            (if (one-positive-integer? params)
  438.            (format:out-fill (car params) slib:form-feed)
  439.            (format:out-char slib:form-feed))
  440.            (set! format:output-col 0)
  441.            (anychar-dispatch))
  442.           ((#\T)            ; Tabulate
  443.            (format:tabulate modifier params)
  444.            (anychar-dispatch))
  445.           ((#\Y)            ; Pretty-print
  446.            (require 'pretty-print)
  447.            (pretty-print (next-arg) format:port)
  448.            (set! format:output-col 0)
  449.            (anychar-dispatch))
  450.           ((#\? #\K)        ; Indirection (is "~K" in T-Scheme)
  451.            (cond
  452.         ((memq modifier '(colon colon-at))
  453.          (format:error "illegal modifier in ~~?"))
  454.         ((eq? modifier 'at)
  455.          (let* ((frmt (next-arg))
  456.             (args (rest-args)))
  457.            (add-arg-pos (format:format-work frmt args))))
  458.         (else
  459.          (let* ((frmt (next-arg))
  460.             (args (next-arg)))
  461.            (format:format-work frmt args))))
  462.            (anychar-dispatch))
  463.           ((#\!)            ; Flush output
  464.            (set! format:flush-output #t)
  465.            (anychar-dispatch))
  466.           ((#\newline)        ; Continuation lines
  467.            (if (eq? modifier 'at)
  468.            (format:out-char #\newline))
  469.            (if (< format:pos format-string-len)
  470.            (do ((ch (peek-next-char) (peek-next-char)))
  471.                ((or (not (char-whitespace? ch))
  472.                 (= format:pos (- format-string-len 1))))
  473.              (if (eq? modifier 'colon)
  474.              (format:out-char (next-char))
  475.              (next-char))))
  476.            (anychar-dispatch))
  477.           ((#\*)            ; Argument jumping
  478.            (case modifier
  479.          ((colon)        ; jump backwards
  480.           (if (one-positive-integer? params)
  481.               (do ((i 0 (+ i 1)))
  482.               ((= i (car params)))
  483.             (prev-arg))
  484.               (prev-arg)))
  485.          ((at)            ; jump absolute
  486.           (set! arg-pos (if (one-positive-integer? params)
  487.                     (car params) 0)))
  488.          ((colon-at)
  489.           (format:error "illegal modifier `:@' in ~~* directive"))
  490.          (else            ; jump forward
  491.           (if (one-positive-integer? params)
  492.               (do ((i 0 (+ i 1)))
  493.               ((= i (car params)))
  494.             (next-arg))
  495.               (next-arg))))
  496.            (anychar-dispatch))
  497.           ((#\()            ; Case conversion begin
  498.            (set! format:case-conversion
  499.              (case modifier
  500.                ((at) format:string-capitalize-first)
  501.                ((colon) string-capitalize)
  502.                ((colon-at) string-upcase)
  503.                (else string-downcase)))
  504.            (anychar-dispatch))
  505.           ((#\))            ; Case conversion end
  506.            (if (not format:case-conversion)
  507.            (format:error "missing ~~("))
  508.            (set! format:case-conversion #f)
  509.            (anychar-dispatch))
  510.           ((#\[)            ; Conditional begin
  511.            (set! conditional-nest (+ conditional-nest 1))
  512.            (cond
  513.         ((= conditional-nest 1)
  514.          (set! clause-pos format:pos)
  515.          (set! clause-default #f)
  516.          (set! clauses '())
  517.          (set! conditional-type
  518.                (case modifier
  519.              ((at) 'if-then)
  520.              ((colon) 'if-else-then)
  521.              ((colon-at) (format:error "illegal modifier in ~~["))
  522.              (else 'num-case)))
  523.          (set! conditional-arg
  524.                (if (one-positive-integer? params)
  525.                (car params)
  526.                (next-arg)))))
  527.            (anychar-dispatch))
  528.           ((#\;)                    ; Conditional separator
  529.            (if (zero? conditional-nest)
  530.            (format:error "~~; not in ~~[~~] conditional"))
  531.            (if (not (null? params))
  532.            (format:error "no parameter allowed in ~~;"))
  533.            (if (= conditional-nest 1)
  534.            (let ((clause-str
  535.               (cond
  536.                ((eq? modifier 'colon)
  537.                 (set! clause-default #t)
  538.                 (substring format-string clause-pos
  539.                        (- format:pos 3)))
  540.                ((memq modifier '(at colon-at))
  541.                 (format:error "illegal modifier in ~~;"))
  542.                (else
  543.                 (substring format-string clause-pos
  544.                        (- format:pos 2))))))
  545.              (set! clauses (append clauses (list clause-str)))
  546.              (set! clause-pos format:pos)))
  547.            (anychar-dispatch))
  548.           ((#\])            ; Conditional end
  549.            (if (zero? conditional-nest) (format:error "missing ~~["))
  550.            (set! conditional-nest (- conditional-nest 1))
  551.            (if modifier
  552.            (format:error "no modifier allowed in ~~]"))
  553.            (if (not (null? params))
  554.            (format:error "no parameter allowed in ~~]"))
  555.            (cond
  556.         ((zero? conditional-nest)
  557.          (let ((clause-str (substring format-string clause-pos
  558.                           (- format:pos 2))))
  559.            (if clause-default
  560.                (set! clause-default clause-str)
  561.                (set! clauses (append clauses (list clause-str)))))
  562.          (case conditional-type
  563.            ((if-then)
  564.             (if conditional-arg
  565.             (format:format-work (car clauses)
  566.                         (list conditional-arg))))
  567.            ((if-else-then)
  568.             (add-arg-pos
  569.              (format:format-work (if conditional-arg
  570.                          (cadr clauses)
  571.                          (car clauses))
  572.                      (rest-args))))
  573.            ((num-case)
  574.             (if (or (not (integer? conditional-arg))
  575.                 (< conditional-arg 0))
  576.             (format:error "argument not a positive integer"))
  577.             (if (not (and (>= conditional-arg (length clauses))
  578.                   (not clause-default)))
  579.             (add-arg-pos
  580.              (format:format-work
  581.               (if (>= conditional-arg (length clauses))
  582.                   clause-default
  583.                   (list-ref clauses conditional-arg))
  584.               (rest-args))))))))
  585.            (anychar-dispatch))
  586.           ((#\{)            ; Iteration begin
  587.            (set! iteration-nest (+ iteration-nest 1))
  588.            (cond
  589.         ((= iteration-nest 1)
  590.          (set! iteration-pos format:pos)
  591.          (set! iteration-type
  592.                (case modifier
  593.              ((at) 'rest-args)
  594.              ((colon) 'sublists)
  595.              ((colon-at) 'rest-sublists)
  596.              (else 'list)))
  597.          (set! max-iterations (if (one-positive-integer? params)
  598.                      (car params) #f))))
  599.            (anychar-dispatch))
  600.           ((#\})            ; Iteration end
  601.            (if (zero? iteration-nest) (format:error "missing ~~{"))
  602.            (set! iteration-nest (- iteration-nest 1))
  603.            (case modifier
  604.          ((colon)
  605.           (if (not max-iterations) (set! max-iterations 1)))
  606.          ((colon-at at) (format:error "illegal modifier"))
  607.          (else (if (not max-iterations) (set! max-iterations 100))))
  608.            (if (not (null? params))
  609.            (format:error "no parameters allowed in ~~}"))
  610.            (if (zero? iteration-nest)
  611.          (let ((iteration-str
  612.             (substring format-string iteration-pos
  613.                    (- format:pos (if modifier 3 2)))))
  614.            (if (string=? iteration-str "")
  615.                (set! iteration-str (next-arg)))
  616.            (case iteration-type
  617.              ((list)
  618.               (let ((args (next-arg))
  619.                 (args-len 0))
  620.             (if (not (list? args))
  621.                 (format:error "expected a list argument"))
  622.             (set! args-len (length args))
  623.             (do ((arg-pos 0 (+ arg-pos
  624.                        (format:format-work
  625.                         iteration-str
  626.                         (list-tail args arg-pos))))
  627.                  (i 0 (+ i 1)))
  628.                 ((or (>= arg-pos args-len)
  629.                  (>= i max-iterations))))))
  630.              ((sublists)
  631.               (let ((args (next-arg))
  632.                 (args-len 0))
  633.             (if (not (list? args))
  634.                 (format:error "expected a list argument"))
  635.             (set! args-len (length args))
  636.             (do ((arg-pos 0 (+ arg-pos 1)))
  637.                 ((or (>= arg-pos args-len)
  638.                  (>= arg-pos max-iterations)))
  639.               (let ((sublist (list-ref args arg-pos)))
  640.                 (if (not (list? sublist))
  641.                 (format:error
  642.                  "expected a list of lists argument"))
  643.                 (format:format-work iteration-str sublist)))))
  644.              ((rest-args)
  645.               (let* ((args (rest-args))
  646.                  (args-len (length args))
  647.                  (usedup-args
  648.                   (do ((arg-pos 0 (+ arg-pos
  649.                          (format:format-work
  650.                           iteration-str
  651.                           (list-tail
  652.                            args arg-pos))))
  653.                    (i 0 (+ i 1)))
  654.                   ((or (>= arg-pos args-len)
  655.                        (>= i max-iterations))
  656.                    arg-pos))))
  657.             (add-arg-pos usedup-args)))
  658.              ((rest-sublists)
  659.               (let* ((args (rest-args))
  660.                  (args-len (length args))
  661.                  (usedup-args
  662.                   (do ((arg-pos 0 (+ arg-pos 1)))
  663.                   ((or (>= arg-pos args-len)
  664.                        (>= arg-pos max-iterations))
  665.                    arg-pos)
  666.                 (let ((sublist (list-ref args arg-pos)))
  667.                   (if (not (list? sublist))
  668.                       (format:error "expected list arguments"))
  669.                   (format:format-work iteration-str sublist)))))
  670.             (add-arg-pos usedup-args)))
  671.              (else (format:error "internal error in ~~}")))))
  672.            (anychar-dispatch))
  673.           ((#\^)            ; Up and out
  674.            (let* ((continue
  675.                (cond
  676.             ((not (null? params))
  677.              (not
  678.               (case (length params)
  679.                ((1) (zero? (car params)))
  680.                ((2) (= (list-ref params 0) (list-ref params 1)))
  681.                ((3) (<= (list-ref params 0)
  682.                     (list-ref params 1)
  683.                     (list-ref params 2)))
  684.                (else (format:error "too much parameters")))))
  685.             (format:case-conversion ; if conversion stop conversion
  686.              (set! format:case-conversion string-copy) #t)
  687.             ((= iteration-nest 1) #t)
  688.             ((= conditional-nest 1) #t)
  689.             ((>= arg-pos arg-len)
  690.              (set! format:pos format-string-len) #f)
  691.             (else #t))))
  692.          (if continue
  693.              (anychar-dispatch))))
  694.  
  695.           ;; format directive modifiers and parameters
  696.  
  697.           ((#\@)            ; `@' modifier
  698.            (if (memq modifier '(at colon-at))
  699.            (format:error "double `@' modifier"))
  700.            (set! modifier (if (eq? modifier 'colon) 'colon-at 'at))
  701.            (tilde-dispatch))
  702.           ((#\:)            ; `:' modifier
  703.            (if (memq modifier '(colon colon-at))
  704.            (format:error "double `:' modifier"))
  705.            (set! modifier (if (eq? modifier 'at) 'colon-at 'colon))
  706.            (tilde-dispatch))
  707.           ((#\')            ; Character parameter
  708.            (if modifier (format:error "misplaced modifier"))
  709.            (set! params (append params (list (char->integer (next-char)))))
  710.            (set! param-value-found #t)
  711.            (tilde-dispatch))
  712.           ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+) ; num. paramtr
  713.            (if modifier (format:error "misplaced modifier"))
  714.            (let ((num-str-beg (- format:pos 1))
  715.              (num-str-end format:pos))
  716.          (do ((ch (peek-next-char) (peek-next-char)))
  717.              ((not (char-numeric? ch)))
  718.            (next-char)
  719.            (set! num-str-end (+ 1 num-str-end)))
  720.          (set! params
  721.                (append params
  722.                    (list (string->number
  723.                       (substring format-string
  724.                          num-str-beg
  725.                          num-str-end))))))
  726.            (set! param-value-found #t)
  727.            (tilde-dispatch))
  728.           ((#\V)            ; Variable parameter from next argum.
  729.            (if modifier (format:error "misplaced modifier"))
  730.            (set! params (append params (list (next-arg))))
  731.            (set! param-value-found #t)
  732.            (tilde-dispatch))
  733.           ((#\#)            ; Parameter is number of remaining args
  734.            (if modifier (format:error "misplaced modifier"))
  735.            (set! params (append params (list (length (rest-args)))))
  736.            (set! param-value-found #t)
  737.            (tilde-dispatch))
  738.           ((#\,)            ; Parameter separators
  739.            (if modifier (format:error "misplaced modifier"))
  740.            (if (not param-value-found)
  741.            (set! params (append params '(#f)))) ; append empty paramtr
  742.            (set! param-value-found #f)
  743.            (tilde-dispatch))
  744.           ((#\Q)            ; Inquiry messages
  745.            (if (eq? modifier 'colon)
  746.            (format:out-str format:version)
  747.            (let ((nl (string #\newline)))
  748.              (format:out-str
  749.               (string-append
  750.                "SLIB Common LISP format version " format:version nl
  751.                "  (C) copyright 1992-1994 by Dirk Lutzebaeck" nl
  752.                "  please send bug reports to `lutzeb@cs.tu-berlin.de'"
  753.                nl))))
  754.            (anychar-dispatch))
  755.           (else            ; Unknown tilde directive
  756.            (format:error "unknown control character `~c'"
  757.               (string-ref format-string (- format:pos 1))))))
  758.        (else (anychar-dispatch)))))) ; in case of conditional
  759.  
  760.     (set! format:pos 0)
  761.     (set! format:arg-pos 0)
  762.     (anychar-dispatch)            ; start the formatting
  763.     (set! format:pos recursive-pos-save)
  764.     arg-pos))                ; return the position in the arg. list
  765.  
  766. ;; format:obj->str returns a R4RS representation as a string of an arbitrary
  767. ;; scheme object.
  768. ;; First parameter is the object, second parameter is a boolean if the
  769. ;; representation should be slashified as `write' does.
  770. ;; It uses format:char->str which converts a character into
  771. ;; a slashified string as `write' does and which is implementation dependent.
  772. ;; It uses format:iobj->str to print out internal objects as
  773. ;; quoted strings so that the output can always be processed by (read)
  774.  
  775. (define (format:obj->str obj slashify)
  776.   (cond
  777.    ((string? obj)
  778.     (if slashify
  779.     (let ((obj-len (string-length obj)))
  780.       (string-append
  781.        "\""
  782.        (let loop ((i 0) (j 0))    ; taken from Marc Feeley's pp.scm
  783.          (if (= j obj-len)
  784.          (string-append (substring obj i j) "\"")
  785.          (let ((c (string-ref obj j)))
  786.            (if (or (char=? c #\\)
  787.                (char=? c #\"))
  788.                (string-append (substring obj i j) "\\"
  789.                       (loop j (+ j 1)))
  790.                (loop i (+ j 1))))))))
  791.     obj))
  792.  
  793.    ((boolean? obj) (if obj "#t" "#f"))
  794.  
  795.    ((number? obj) (number->string obj))
  796.  
  797.    ((symbol? obj)
  798.     (if format:symbol-case-conv
  799.     (format:symbol-case-conv (symbol->string obj))
  800.     (symbol->string obj)))
  801.  
  802.    ((char? obj)
  803.     (if slashify
  804.     (format:char->str obj)
  805.     (string obj)))
  806.  
  807.    ((null? obj) "()")
  808.  
  809.    ((input-port? obj)
  810.     (format:iobj->str obj))
  811.  
  812.    ((output-port? obj)
  813.     (format:iobj->str obj))
  814.  
  815.    ((list? obj)
  816.     (string-append "("
  817.            (let loop ((obj-list obj))
  818.              (if (null? (cdr obj-list))
  819.              (format:obj->str (car obj-list) #t)
  820.              (string-append
  821.               (format:obj->str (car obj-list) #t)
  822.               " "
  823.               (loop (cdr obj-list)))))
  824.            ")"))
  825.  
  826.    ((pair? obj)
  827.     (string-append "("
  828.            (format:obj->str (car obj) #t)
  829.            " . "
  830.            (format:obj->str (cdr obj) #t)
  831.            ")"))
  832.  
  833.    ((vector? obj)
  834.     (string-append "#" (format:obj->str (vector->list obj) #t)))
  835.  
  836.    (else                ; only objects with an #<...>
  837.     (format:iobj->str obj))))        ; representation should fall in here
  838.  
  839. ;; format:iobj->str reveals the implementation dependent representation of
  840. ;; #<...> objects with the use of display and call-with-output-string.
  841. ;; If format:read-proof is set to #t the resulting string is additionally
  842. ;; set into string quotes.
  843.  
  844. (define format:read-proof #f)
  845.  
  846. (define (format:iobj->str iobj)
  847.   (if (or format:read-proof
  848.       format:iobj-case-conv)
  849.       (string-append
  850.        (if format:read-proof "\"" "")
  851.        (if format:iobj-case-conv
  852.        (format:iobj-case-conv
  853.         (call-with-output-string (lambda (p) (display iobj p))))
  854.        (call-with-output-string (lambda (p) (display iobj p))))
  855.        (if format:read-proof "\"" ""))
  856.       (call-with-output-string (lambda (p) (display iobj p)))))
  857.  
  858.  
  859. ;; format:char->str converts a character into a slashified string as
  860. ;; done by `write'. The procedure is dependent on the integer
  861. ;; representation of characters and assumes a character number according to
  862. ;; the ASCII character set.
  863.  
  864. (define (format:char->str ch)
  865.   (let ((int-rep (char->integer ch)))
  866.     (if (< int-rep 0)            ; if chars are [-128...+127]
  867.     (set! int-rep (+ int-rep 256)))
  868.     (string-append
  869.      "#\\"
  870.      (cond
  871.       ((char=? ch #\newline) "newline")
  872.       ((and (>= int-rep 0) (<= int-rep 32))
  873.        (vector-ref format:ascii-non-printable-charnames int-rep))
  874.       ((= int-rep 127) "del")
  875.       ((>= int-rep 128)        ; octal representation
  876.        (if format:radix-pref
  877.        (let ((s (number->string int-rep 8)))
  878.          (substring s 2 (string-length s)))
  879.        (number->string int-rep 8)))
  880.       (else (string ch))))))
  881.  
  882. (define format:space-ch (char->integer #\space))
  883. (define format:zero-ch (char->integer #\0))
  884.  
  885. (define (format:par pars length index default name)
  886.   (if (> length index)
  887.       (let ((par (list-ref pars index)))
  888.     (if par
  889.         (if name
  890.         (if (< par 0)
  891.             (format:error
  892.              "~s parameter must be a positive integer" name)
  893.             par)
  894.         par)
  895.         default))
  896.       default))
  897.  
  898. (define (format:out-obj-padded pad-left obj slashify pars)
  899.   (if (null? pars)
  900.       (format:out-str (format:obj->str obj slashify))
  901.       (let ((l (length pars)))
  902.     (let ((mincol (format:par pars l 0 0 "mincol"))
  903.           (colinc (format:par pars l 1 1 "colinc"))
  904.           (minpad (format:par pars l 2 0 "minpad"))
  905.           (padchar (integer->char
  906.             (format:par pars l 3 format:space-ch #f)))
  907.           (objstr (format:obj->str obj slashify)))
  908.       (if (not pad-left)
  909.           (format:out-str objstr))
  910.       (do ((objstr-len (string-length objstr))
  911.            (i minpad (+ i colinc)))
  912.           ((>= (+ objstr-len i) mincol)
  913.            (format:out-fill i padchar)))
  914.       (if pad-left
  915.           (format:out-str objstr))))))
  916.  
  917. (define (format:out-num-padded modifier number pars radix)
  918.   (if (not (integer? number)) (format:error "argument not an integer"))
  919.   (let ((numstr (number->string number radix)))
  920.     (if (and format:radix-pref (not (= radix 10)))
  921.     (set! numstr (substring numstr 2 (string-length numstr))))
  922.     (if (and (null? pars) (not modifier))
  923.     (format:out-str numstr)
  924.     (let ((l (length pars))
  925.           (numstr-len (string-length numstr)))
  926.       (let ((mincol (format:par pars l 0 #f "mincol"))
  927.         (padchar (integer->char
  928.               (format:par pars l 1 format:space-ch #f)))
  929.         (commachar (integer->char
  930.                 (format:par pars l 2 (char->integer #\,) #f)))
  931.         (commawidth (format:par pars l 3 3 "commawidth")))
  932.         (if mincol
  933.         (let ((numlen numstr-len)) ; calc. the output len of number
  934.           (if (and (memq modifier '(at colon-at)) (> number 0))
  935.               (set! numlen (+ numlen 1)))
  936.           (if (memq modifier '(colon colon-at))
  937.               (set! numlen (+ (quotient (- numstr-len
  938.                            (if (< number 0) 2 1))
  939.                         commawidth)
  940.                       numlen)))
  941.           (if (> mincol numlen)
  942.               (format:out-fill (- mincol numlen) padchar))))
  943.         (if (and (memq modifier '(at colon-at))
  944.              (> number 0))
  945.         (format:out-char #\+))
  946.         (if (memq modifier '(colon colon-at)) ; insert comma character
  947.         (let ((start (remainder numstr-len commawidth))
  948.               (ns (if (< number 0) 1 0)))
  949.           (format:out-substr numstr 0 start)
  950.           (do ((i start (+ i commawidth)))
  951.               ((>= i numstr-len))
  952.             (if (> i ns)
  953.             (format:out-char commachar))
  954.             (format:out-substr numstr i (+ i commawidth))))
  955.         (format:out-str numstr)))))))
  956.  
  957. (define (format:tabulate modifier pars)
  958.   (let ((l (length pars)))
  959.     (let ((colnum (format:par pars l 0 1 "colnum"))
  960.       (colinc (format:par pars l 1 1 "colinc"))
  961.       (padch (integer->char (format:par pars l 2 format:space-ch #f))))
  962.       (case modifier
  963.     ((colon colon-at)
  964.      (format:error "unsupported modifier for ~~t"))
  965.     ((at)                ; relative tabulation
  966.      (format:out-fill
  967.       (if (= colinc 0)
  968.           colnum            ; colnum = colrel
  969.           (do ((c 0 (+ c colinc))
  970.            (col (+ format:output-col colnum)))
  971.           ((>= c col)
  972.            (- c format:output-col))))
  973.       padch))
  974.     (else                ; absolute tabulation
  975.      (format:out-fill
  976.       (cond
  977.        ((< format:output-col colnum)
  978.         (- colnum format:output-col))
  979.        ((= colinc 0)
  980.         0)
  981.        (else
  982.         (do ((c colnum (+ c colinc)))
  983.         ((>= c format:output-col)
  984.          (- c format:output-col)))))
  985.       padch))))))
  986.  
  987.  
  988. ;; roman numerals (from dorai@cs.rice.edu).
  989.  
  990. (define format:roman-alist
  991.   '((1000 #\M) (500 #\D) (100 #\C) (50 #\L)
  992.     (10 #\X) (5 #\V) (1 #\I)))
  993.  
  994. (define format:roman-boundary-values
  995.   '(100 100 10 10 1 1 #f))
  996.  
  997. (define format:num->old-roman
  998.   (lambda (n)
  999.     (if (and (integer? n) (>= n 1))
  1000.     (let loop ((n n)
  1001.            (romans format:roman-alist)
  1002.            (s '()))
  1003.       (if (null? romans) (list->string (reverse s))
  1004.           (let ((roman-val (caar romans))
  1005.             (roman-dgt (cadar romans)))
  1006.         (do ((q (quotient n roman-val) (- q 1))
  1007.              (s s (cons roman-dgt s)))
  1008.             ((= q 0)
  1009.              (loop (remainder n roman-val)
  1010.                (cdr romans) s))))))
  1011.     (format:error "only positive integers can be romanized"))))
  1012.  
  1013. (define format:num->roman
  1014.   (lambda (n)
  1015.     (if (and (integer? n) (> n 0))
  1016.     (let loop ((n n)
  1017.            (romans format:roman-alist)
  1018.            (boundaries format:roman-boundary-values)
  1019.            (s '()))
  1020.       (if (null? romans)
  1021.           (list->string (reverse s))
  1022.           (let ((roman-val (caar romans))
  1023.             (roman-dgt (cadar romans))
  1024.             (bdry (car boundaries)))
  1025.         (let loop2 ((q (quotient n roman-val))
  1026.                 (r (remainder n roman-val))
  1027.                 (s s))
  1028.           (if (= q 0)
  1029.               (if (and bdry (>= r (- roman-val bdry)))
  1030.               (loop (remainder r bdry) (cdr romans)
  1031.                 (cdr boundaries)
  1032.                 (cons roman-dgt
  1033.                   (append
  1034.                 (cdr (assv bdry romans))
  1035.                 s)))
  1036.               (loop r (cdr romans) (cdr boundaries) s))
  1037.               (loop2 (- q 1) r (cons roman-dgt s)))))))
  1038.     (format:error "only positive integers can be romanized"))))
  1039.  
  1040. ;; cardinals & ordinals (from dorai@cs.rice.edu)
  1041.  
  1042. (define format:cardinal-ones-list
  1043.   '(#f "one" "two" "three" "four" "five"
  1044.      "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen"
  1045.      "fourteen" "fifteen" "sixteen" "seventeen" "eighteen"
  1046.      "nineteen"))
  1047.  
  1048. (define format:cardinal-tens-list
  1049.   '(#f #f "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty"
  1050.      "ninety"))
  1051.  
  1052. (define format:num->cardinal999
  1053.   (lambda (n)
  1054.     ;this procedure is inspired by the Bruno Haible's CLisp
  1055.     ;function format-small-cardinal, which converts numbers
  1056.     ;in the range 1 to 999, and is used for converting each
  1057.     ;thousand-block in a larger number
  1058.     (let* ((hundreds (quotient n 100))
  1059.        (tens+ones (remainder n 100))
  1060.        (tens (quotient tens+ones 10))
  1061.        (ones (remainder tens+ones 10)))
  1062.       (append
  1063.     (if (> hundreds 0)
  1064.         (append
  1065.           (string->list
  1066.         (list-ref format:cardinal-ones-list hundreds))
  1067.           (string->list" hundred")
  1068.           (if (> tens+ones 0) '(#\space) '()))
  1069.         '())
  1070.     (if (< tens+ones 20)
  1071.         (if (> tens+ones 0)
  1072.         (string->list
  1073.           (list-ref format:cardinal-ones-list tens+ones))
  1074.         '())
  1075.         (append
  1076.           (string->list
  1077.         (list-ref format:cardinal-tens-list tens))
  1078.           (if (> ones 0)
  1079.           (cons #\-
  1080.             (string->list
  1081.               (list-ref format:cardinal-ones-list ones)))
  1082.           '())))))))
  1083.  
  1084. (define format:cardinal-thousand-block-list
  1085.   '("" " thousand" " million" " billion" " trillion" " quadrillion"
  1086.      " quintillion" " sextillion" " septillion" " octillion" " nonillion"
  1087.      " decillion" " undecillion" " duodecillion" " tredecillion"
  1088.      " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
  1089.      " octodecillion" " novemdecillion" " vigintillion"))
  1090.  
  1091. (define format:num->cardinal
  1092.   (lambda (n)
  1093.     (cond ((not (integer? n))
  1094.        (format:error
  1095.          "only integers can be converted to English cardinals"))
  1096.       ((= n 0) "zero")
  1097.       ((< n 0) (string-append "minus " (format:num->cardinal (- n))))
  1098.       (else
  1099.         (let ((power3-word-limit
  1100.             (length format:cardinal-thousand-block-list)))
  1101.           (let loop ((n n)
  1102.              (power3 0)
  1103.              (s '()))
  1104.         (if (= n 0)
  1105.             (list->string s)
  1106.             (let ((n-before-block (quotient n 1000))
  1107.               (n-after-block (remainder n 1000)))
  1108.               (loop n-before-block
  1109.             (+ power3 1)
  1110.             (if (> n-after-block 0)
  1111.                 (append
  1112.                   (if (> n-before-block 0)
  1113.                   (string->list ", ") '())
  1114.                   (format:num->cardinal999 n-after-block)
  1115.                   (if (< power3 power3-word-limit)
  1116.                   (string->list
  1117.                     (list-ref
  1118.                      format:cardinal-thousand-block-list
  1119.                      power3))
  1120.                   (append
  1121.                     (string->list " times ten to the ")
  1122.                     (string->list
  1123.                       (format:num->ordinal
  1124.                     (* power3 3)))
  1125.                     (string->list " power")))
  1126.                   s)
  1127.                 s))))))))))
  1128.  
  1129. (define format:ordinal-ones-list
  1130.   '(#f "first" "second" "third" "fourth" "fifth"
  1131.      "sixth" "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth"
  1132.      "thirteenth" "fourteenth" "fifteenth" "sixteenth" "seventeenth"
  1133.      "eighteenth" "nineteenth"))
  1134.  
  1135. (define format:ordinal-tens-list
  1136.   '(#f #f "twentieth" "thirtieth" "fortieth" "fiftieth" "sixtieth"
  1137.      "seventieth" "eightieth" "ninetieth"))
  1138.  
  1139. (define format:num->ordinal
  1140.   (lambda (n)
  1141.     (cond ((not (integer? n))
  1142.        (format:error
  1143.          "only integers can be converted to English ordinals"))
  1144.       ((= n 0) "zeroth")
  1145.       ((< n 0) (string-append "minus " (format:num->ordinal (- n))))
  1146.       (else
  1147.         (let ((hundreds (quotient n 100))
  1148.           (tens+ones (remainder n 100)))
  1149.           (string-append
  1150.         (if (> hundreds 0)
  1151.             (string-append
  1152.               (format:num->cardinal (* hundreds 100))
  1153.               (if (= tens+ones 0) "th" " "))
  1154.             "")
  1155.         (if (= tens+ones 0) ""
  1156.             (if (< tens+ones 20)
  1157.             (list-ref format:ordinal-ones-list tens+ones)
  1158.             (let ((tens (quotient tens+ones 10))
  1159.                   (ones (remainder tens+ones 10)))
  1160.               (if (= ones 0)
  1161.                   (list-ref format:ordinal-tens-list tens)
  1162.                   (string-append
  1163.                 (list-ref format:cardinal-tens-list tens)
  1164.                 "-"
  1165.                 (list-ref format:ordinal-ones-list ones))))
  1166.             ))))))))
  1167.  
  1168. ;; format fixed flonums (~F)
  1169.  
  1170. (define (format:out-fixed modifier number pars)
  1171.   (if (not (or (number? number) (string? number)))
  1172.       (format:error "argument is not a number or a number string"))
  1173.  
  1174.   (let ((l (length pars)))
  1175.     (let ((width (format:par pars l 0 #f "width"))
  1176.       (digits (format:par pars l 1 #f "digits"))
  1177.       (scale (format:par pars l 2 0 #f))
  1178.       (overch (format:par pars l 3 #f #f))
  1179.       (padch (format:par pars l 4 format:space-ch #f)))
  1180.  
  1181.     (if digits
  1182.  
  1183.     (begin                ; fixed precision
  1184.       (format:parse-float
  1185.        (if (string? number) number (number->string number)) #t scale)
  1186.       (if (<= (- format:fn-len format:fn-dot) digits)
  1187.           (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1188.           (format:fn-round digits))
  1189.       (if width
  1190.           (let ((numlen (+ format:fn-len 1)))
  1191.         (if (or (not format:fn-pos?) (eq? modifier 'at))
  1192.             (set! numlen (+ numlen 1)))
  1193.         (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1194.             (set! numlen (+ numlen 1)))
  1195.         (if (< numlen width)
  1196.             (format:out-fill (- width numlen) (integer->char padch)))
  1197.         (if (and overch (> numlen width))
  1198.             (format:out-fill width (integer->char overch))
  1199.             (format:fn-out modifier (> width (+ digits 1)))))
  1200.           (format:fn-out modifier #t)))
  1201.  
  1202.     (begin                ; free precision
  1203.       (format:parse-float
  1204.        (if (string? number) number (number->string number)) #t scale)
  1205.       (format:fn-strip)
  1206.       (if width
  1207.           (let ((numlen (+ format:fn-len 1)))
  1208.         (if (or (not format:fn-pos?) (eq? modifier 'at))
  1209.             (set! numlen (+ numlen 1)))
  1210.         (if (= format:fn-dot 0)
  1211.             (set! numlen (+ numlen 1)))
  1212.         (if (< numlen width)
  1213.             (format:out-fill (- width numlen) (integer->char padch)))
  1214.         (if (> numlen width)    ; adjust precision if possible
  1215.             (let ((dot-index (- numlen
  1216.                     (- format:fn-len format:fn-dot))))
  1217.               (if (> dot-index width)
  1218.               (if overch    ; numstr too big for required width
  1219.                   (format:out-fill width (integer->char overch))
  1220.                   (format:fn-out modifier #t))
  1221.               (begin
  1222.                 (format:fn-round (- width dot-index))
  1223.                 (format:fn-out modifier #t))))
  1224.             (format:fn-out modifier #t)))
  1225.           (format:fn-out modifier #t)))))))
  1226.  
  1227. ;; format exponential flonums (~E)
  1228.  
  1229. (define (format:out-expon modifier number pars)
  1230.   (if (not (or (number? number) (string? number)))
  1231.       (format:error "argument is not a number"))
  1232.  
  1233.   (let ((l (length pars)))
  1234.     (let ((width (format:par pars l 0 #f "width"))
  1235.       (digits (format:par pars l 1 #f "digits"))
  1236.       (edigits (format:par pars l 2 #f "exponent digits"))
  1237.       (scale (format:par pars l 3 1 #f))
  1238.       (overch (format:par pars l 4 #f #f))
  1239.       (padch (format:par pars l 5 format:space-ch #f))
  1240.       (expch (format:par pars l 6 #f #f)))
  1241.  
  1242.     (if digits                ; fixed precision
  1243.  
  1244.     (let ((digits (if (> scale 0)
  1245.               (if (< scale (+ digits 2))
  1246.                   (+ (- digits scale) 1)
  1247.                   0)
  1248.               digits)))
  1249.       (format:parse-float
  1250.        (if (string? number) number (number->string number)) #f scale)
  1251.       (if (<= (- format:fn-len format:fn-dot) digits)
  1252.           (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1253.           (format:fn-round digits))
  1254.       (if width
  1255.           (if (and edigits overch (> format:en-len edigits))
  1256.           (format:out-fill width (integer->char overch))
  1257.           (let ((numlen (+ format:fn-len 3))) ; .E+
  1258.             (if (or (not format:fn-pos?) (eq? modifier 'at))
  1259.             (set! numlen (+ numlen 1)))
  1260.             (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1261.             (set! numlen (+ numlen 1)))
  1262.             (set! numlen
  1263.               (+ numlen
  1264.                  (if (and edigits (>= edigits format:en-len))
  1265.                  edigits
  1266.                  format:en-len)))
  1267.             (if (< numlen width)
  1268.             (format:out-fill (- width numlen)
  1269.                      (integer->char padch)))
  1270.             (if (and overch (> numlen width))
  1271.             (format:out-fill width (integer->char overch))
  1272.             (begin
  1273.               (format:fn-out modifier (> width (- numlen 1)))
  1274.               (format:en-out edigits expch)))))
  1275.           (begin
  1276.         (format:fn-out modifier #t)
  1277.         (format:en-out edigits expch))))
  1278.  
  1279.     (begin                ; free precision
  1280.       (format:parse-float
  1281.        (if (string? number) number (number->string number)) #f scale)
  1282.       (format:fn-strip)
  1283.       (if width
  1284.           (if (and edigits overch (> format:en-len edigits))
  1285.           (format:out-fill width (integer->char overch))
  1286.           (let ((numlen (+ format:fn-len 3))) ; .E+
  1287.             (if (or (not format:fn-pos?) (eq? modifier 'at))
  1288.             (set! numlen (+ numlen 1)))
  1289.             (if (= format:fn-dot 0)
  1290.             (set! numlen (+ numlen 1)))
  1291.             (set! numlen
  1292.               (+ numlen
  1293.                  (if (and edigits (>= edigits format:en-len))
  1294.                  edigits
  1295.                  format:en-len)))
  1296.             (if (< numlen width)
  1297.             (format:out-fill (- width numlen)
  1298.                      (integer->char padch)))
  1299.             (if (> numlen width) ; adjust precision if possible
  1300.             (let ((f (- format:fn-len format:fn-dot))) ; fract len
  1301.               (if (> (- numlen f) width)
  1302.                   (if overch ; numstr too big for required width
  1303.                   (format:out-fill width
  1304.                            (integer->char overch))
  1305.                   (begin
  1306.                     (format:fn-out modifier #t)
  1307.                     (format:en-out edigits expch)))
  1308.                   (begin
  1309.                 (format:fn-round (+ (- f numlen) width))
  1310.                 (format:fn-out modifier #t)
  1311.                 (format:en-out edigits expch))))
  1312.             (begin
  1313.               (format:fn-out modifier #t)
  1314.               (format:en-out edigits expch)))))
  1315.           (begin
  1316.         (format:fn-out modifier #t)
  1317.         (format:en-out edigits expch))))))))
  1318.  
  1319. ;; format general flonums (~G)
  1320.  
  1321. (define (format:out-general modifier number pars)
  1322.   (if (not (or (number? number) (string? number)))
  1323.       (format:error "argument is not a number or a number string"))
  1324.  
  1325.   (let ((l (length pars)))
  1326.     (let ((width (if (> l 0) (list-ref pars 0) #f))
  1327.       (digits (if (> l 1) (list-ref pars 1) #f))
  1328.       (edigits (if (> l 2) (list-ref pars 2) #f))
  1329.       (overch (if (> l 4) (list-ref pars 4) #f))
  1330.       (padch (if (> l 5) (list-ref pars 5) #f)))
  1331.     (format:parse-float
  1332.      (if (string? number) number (number->string number)) #t 0)
  1333.     (format:fn-strip)
  1334.     (let* ((ee (if edigits (+ edigits 2) 4)) ; for the following algorithm
  1335.        (ww (if width (- width ee) #f))   ; see Steele's CL book p.395
  1336.        (n (if (= format:fn-dot 0)    ; number less than (abs 1.0) ?
  1337.           (- (format:fn-zlead))
  1338.           format:fn-dot))
  1339.        (d (if digits
  1340.           digits
  1341.           (max format:fn-len (min n 7)))) ; q = format:fn-len
  1342.        (dd (- d n)))
  1343.       (if (<= 0 dd d)
  1344.       (begin
  1345.         (format:out-fixed modifier number (list ww dd #f overch padch))
  1346.         (format:out-fill ee #\space)) ;~@T not implemented yet
  1347.       (format:out-expon modifier number pars))))))
  1348.  
  1349. ;; format dollar flonums (~$)
  1350.  
  1351. (define (format:out-dollar modifier number pars)
  1352.   (if (not (or (number? number) (string? number)))
  1353.       (format:error "argument is not a number or a number string"))
  1354.  
  1355.   (let ((l (length pars)))
  1356.     (let ((digits (format:par pars l 0 2 "digits"))
  1357.       (mindig (format:par pars l 1 1 "mindig"))
  1358.       (width (format:par pars l 2 0 "width"))
  1359.       (padch (format:par pars l 3 format:space-ch #f)))
  1360.  
  1361.     (format:parse-float
  1362.      (if (string? number) number (number->string number)) #t 0)
  1363.     (if (<= (- format:fn-len format:fn-dot) digits)
  1364.     (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1365.     (format:fn-round digits))
  1366.     (let ((numlen (+ format:fn-len 1)))
  1367.       (if (or (not format:fn-pos?) (memq modifier '(at colon-at)))
  1368.       (set! numlen (+ numlen 1)))
  1369.       (if (and mindig (> mindig format:fn-dot))
  1370.       (set! numlen (+ numlen (- mindig format:fn-dot))))
  1371.       (if (and (= format:fn-dot 0) (not mindig))
  1372.       (set! numlen (+ numlen 1)))
  1373.       (if (< numlen width)
  1374.       (case modifier
  1375.         ((colon)
  1376.          (if (not format:fn-pos?)
  1377.          (format:out-char #\-))
  1378.          (format:out-fill (- width numlen) (integer->char padch)))
  1379.         ((at)
  1380.          (format:out-fill (- width numlen) (integer->char padch))
  1381.          (format:out-char (if format:fn-pos? #\+ #\-)))
  1382.         ((colon-at)
  1383.          (format:out-char (if format:fn-pos? #\+ #\-))
  1384.          (format:out-fill (- width numlen) (integer->char padch)))
  1385.         (else
  1386.          (format:out-fill (- width numlen) (integer->char padch))
  1387.          (if (not format:fn-pos?)
  1388.          (format:out-char #\-))))
  1389.       (if format:fn-pos?
  1390.           (if (memq modifier '(at colon-at)) (format:out-char #\+))
  1391.           (format:out-char #\-))))
  1392.     (if (and mindig (> mindig format:fn-dot))
  1393.     (format:out-fill (- mindig format:fn-dot) #\0))
  1394.     (if (and (= format:fn-dot 0) (not mindig))
  1395.     (format:out-char #\0))
  1396.     (format:out-substr format:fn-str 0 format:fn-dot)
  1397.     (format:out-char #\.)
  1398.     (format:out-substr format:fn-str format:fn-dot format:fn-len))))
  1399.  
  1400. ; the flonum buffers
  1401.  
  1402. (define format:fn-max 200)        ; max. number of number digits
  1403. (define format:fn-str (make-string format:fn-max)) ; number buffer
  1404. (define format:fn-len 0)        ; digit length of number
  1405. (define format:fn-dot #f)        ; dot position of number
  1406. (define format:fn-pos? #t)        ; number positive?
  1407. (define format:en-max 10)        ; max. number of exponent digits
  1408. (define format:en-str (make-string format:en-max)) ; exponent buffer
  1409. (define format:en-len 0)        ; digit length of exponent
  1410. (define format:en-pos? #t)        ; exponent positive?
  1411.  
  1412. (define (format:parse-float num-str fixed? scale)
  1413.   (set! format:fn-pos? #t)
  1414.   (set! format:fn-len 0)
  1415.   (set! format:fn-dot #f)
  1416.   (set! format:en-pos? #t)
  1417.   (set! format:en-len 0)
  1418.   (do ((i 0 (+ i 1))
  1419.        (left-zeros 0)
  1420.        (mantissa? #t)
  1421.        (all-zeros? #t)
  1422.        (num-len (string-length num-str))
  1423.        (c #f))            ; current exam. character in num-str
  1424.       ((= i num-len)
  1425.        (if (not format:fn-dot)
  1426.        (set! format:fn-dot format:fn-len))
  1427.  
  1428.        (if all-zeros?
  1429.        (begin
  1430.          (set! left-zeros 0)
  1431.          (set! format:fn-dot 0)
  1432.          (set! format:fn-len 1)))
  1433.  
  1434.        ;; now format the parsed values according to format's need
  1435.  
  1436.        (if fixed?
  1437.  
  1438.        (begin            ; fixed format m.nnn or .nnn
  1439.          (if (and (> left-zeros 0) (> format:fn-dot 0))
  1440.          (if (> format:fn-dot left-zeros)
  1441.              (begin        ; norm 0{0}nn.mm to nn.mm
  1442.                (format:fn-shiftleft left-zeros)
  1443.                (set! left-zeros 0)
  1444.                (set! format:fn-dot (- format:fn-dot left-zeros)))
  1445.              (begin        ; normalize 0{0}.nnn to .nnn
  1446.                (format:fn-shiftleft format:fn-dot)
  1447.                (set! left-zeros (- left-zeros format:fn-dot))
  1448.                (set! format:fn-dot 0))))
  1449.          (if (or (not (= scale 0)) (> format:en-len 0))
  1450.          (let ((shift (+ scale (format:en-int))))
  1451.            (cond
  1452.             (all-zeros? #t)
  1453.             ((> (+ format:fn-dot shift) format:fn-len)
  1454.              (format:fn-zfill
  1455.               #f (- shift (- format:fn-len format:fn-dot)))
  1456.              (set! format:fn-dot format:fn-len))
  1457.             ((< (+ format:fn-dot shift) 0)
  1458.              (format:fn-zfill #t (- (- shift) format:fn-dot))
  1459.              (set! format:fn-dot 0))
  1460.             (else
  1461.              (if (> left-zeros 0)
  1462.              (if (<= left-zeros shift) ; shift always > 0 here
  1463.                  (format:fn-shiftleft shift) ; shift out 0s
  1464.                  (begin
  1465.                    (format:fn-shiftleft left-zeros)
  1466.                    (set! format:fn-dot (- shift left-zeros))))
  1467.              (set! format:fn-dot (+ format:fn-dot shift))))))))
  1468.  
  1469.        (let ((negexp        ; expon format m.nnnEee
  1470.           (if (> left-zeros 0)
  1471.               (- left-zeros format:fn-dot -1)
  1472.               (if (= format:fn-dot 0) 1 0))))
  1473.          (if (> left-zeros 0)
  1474.          (begin            ; normalize 0{0}.nnn to n.nn
  1475.            (format:fn-shiftleft left-zeros)
  1476.            (set! format:fn-dot 1))
  1477.          (if (= format:fn-dot 0)
  1478.              (set! format:fn-dot 1)))
  1479.          (format:en-set (- (+ (- format:fn-dot scale) (format:en-int))
  1480.                    negexp))
  1481.          (cond
  1482.           (all-zeros?
  1483.            (format:en-set 0)
  1484.            (set! format:fn-dot 1))
  1485.           ((< scale 0)        ; leading zero
  1486.            (format:fn-zfill #t (- scale))
  1487.            (set! format:fn-dot 0))
  1488.           ((> scale format:fn-dot)
  1489.            (format:fn-zfill #f (- scale format:fn-dot))
  1490.            (set! format:fn-dot scale))
  1491.           (else
  1492.            (set! format:fn-dot scale)))))
  1493.        #t)
  1494.  
  1495.     ;; do body
  1496.     (set! c (string-ref num-str i))    ; parse the output of number->string
  1497.     (cond                ; which can be any valid number
  1498.      ((char-numeric? c)            ; representation of R4RS except
  1499.       (if mantissa?            ; complex numbers
  1500.       (begin
  1501.         (if (char=? c #\0)
  1502.         (if all-zeros?
  1503.             (set! left-zeros (+ left-zeros 1)))
  1504.         (begin
  1505.           (set! all-zeros? #f)))
  1506.         (string-set! format:fn-str format:fn-len c)
  1507.         (set! format:fn-len (+ format:fn-len 1)))
  1508.       (begin
  1509.         (string-set! format:en-str format:en-len c)
  1510.         (set! format:en-len (+ format:en-len 1)))))
  1511.      ((or (char=? c #\-) (char=? c #\+))
  1512.       (if mantissa?
  1513.       (set! format:fn-pos? (char=? c #\+))
  1514.       (set! format:en-pos? (char=? c #\+))))
  1515.      ((char=? c #\.)
  1516.       (set! format:fn-dot format:fn-len))
  1517.      ((char=? c #\e)
  1518.       (set! mantissa? #f))
  1519.      ((char=? c #\E)
  1520.       (set! mantissa? #f))
  1521.      ((char-whitespace? c) #t)
  1522.      ((char=? c #\d) #t)        ; decimal radix prefix
  1523.      ((char=? c #\#) #t)
  1524.      (else
  1525.       (format:error "illegal character `~c' in number->string" c)))))
  1526.  
  1527. (define (format:en-int)            ; convert exponent string to integer
  1528.   (if (= format:en-len 0)
  1529.       0
  1530.       (do ((i 0 (+ i 1))
  1531.        (n 0))
  1532.       ((= i format:en-len)
  1533.        (if format:en-pos?
  1534.            n
  1535.            (- n)))
  1536.     (set! n (+ (* n 10) (- (char->integer (string-ref format:en-str i))
  1537.                    format:zero-ch))))))
  1538.  
  1539. (define (format:en-set en)        ; set exponent string number
  1540.   (set! format:en-len 0)
  1541.   (set! format:en-pos? (>= en 0))
  1542.   (let ((en-str (number->string en)))
  1543.     (do ((i 0 (+ i 1))
  1544.      (en-len (string-length en-str))
  1545.      (c #f))
  1546.     ((= i en-len))
  1547.       (set! c (string-ref en-str i))
  1548.       (if (char-numeric? c)
  1549.       (begin
  1550.         (string-set! format:en-str format:en-len c)
  1551.         (set! format:en-len (+ format:en-len 1)))))))
  1552.  
  1553. (define (format:fn-zfill left? n)    ; fill current number string with 0s
  1554.   (if (> (+ n format:fn-len) format:fn-max) ; from the left or right
  1555.       (format:error "number is too long to format (enlarge format:fn-max)"))
  1556.   (set! format:fn-len (+ format:fn-len n))
  1557.   (if left?
  1558.       (do ((i format:fn-len (- i 1)))    ; fill n 0s to left
  1559.       ((< i 0))
  1560.     (string-set! format:fn-str i
  1561.              (if (< i n)
  1562.              #\0
  1563.              (string-ref format:fn-str (- i n)))))
  1564.       (do ((i (- format:fn-len n) (+ i 1))) ; fill n 0s to the right
  1565.       ((= i format:fn-len))
  1566.     (string-set! format:fn-str i #\0))))
  1567.  
  1568. (define (format:fn-shiftleft n)        ; shift left current number n positions
  1569.   (if (> n format:fn-len)
  1570.       (format:error "internal error in format:fn-shiftleft (~d,~d)"
  1571.             n format:fn-len))
  1572.   (do ((i n (+ i 1)))
  1573.       ((= i format:fn-len)
  1574.        (set! format:fn-len (- format:fn-len n)))
  1575.     (string-set! format:fn-str (- i n) (string-ref format:fn-str i))))
  1576.  
  1577. (define (format:fn-round digits)    ; round format:fn-str
  1578.   (set! digits (+ digits format:fn-dot))
  1579.   (do ((i digits (- i 1))        ; "099",2 -> "10"
  1580.        (c 5))                ; "023",2 -> "02"
  1581.       ((or (= c 0) (< i 0))        ; "999",2 -> "100"
  1582.        (if (= c 1)            ; "005",2 -> "01"
  1583.        (begin            ; carry overflow
  1584.          (set! format:fn-len digits)
  1585.          (format:fn-zfill #t 1)    ; add a 1 before fn-str
  1586.          (string-set! format:fn-str 0 #\1)
  1587.          (set! format:fn-dot (+ format:fn-dot 1)))
  1588.        (set! format:fn-len digits)))
  1589.     (set! c (+ (- (char->integer (string-ref format:fn-str i))
  1590.           format:zero-ch) c))
  1591.     (string-set! format:fn-str i (integer->char
  1592.                   (if (< c 10)
  1593.                       (+ c format:zero-ch)
  1594.                       (+ (- c 10) format:zero-ch))))
  1595.     (set! c (if (< c 10) 0 1))))
  1596.  
  1597. (define (format:fn-out modifier add-leading-zero?)
  1598.   (if format:fn-pos?
  1599.       (if (eq? modifier 'at)
  1600.       (format:out-char #\+))
  1601.       (format:out-char #\-))
  1602.   (if (= format:fn-dot 0)
  1603.       (if add-leading-zero?
  1604.       (format:out-char #\0))
  1605.       (format:out-substr format:fn-str 0 format:fn-dot))
  1606.   (format:out-char #\.)
  1607.   (format:out-substr format:fn-str format:fn-dot format:fn-len))
  1608.  
  1609. (define (format:en-out edigits expch)
  1610.   (format:out-char (if expch (integer->char expch) format:expch))
  1611.   (format:out-char (if format:en-pos? #\+ #\-))
  1612.   (if edigits
  1613.       (if (< format:en-len edigits)
  1614.       (format:out-fill (- edigits format:en-len) #\0)))
  1615.   (format:out-substr format:en-str 0 format:en-len))
  1616.  
  1617. (define (format:fn-strip)        ; strip trailing zeros but one
  1618.   (string-set! format:fn-str format:fn-len #\0)
  1619.   (do ((i format:fn-len (- i 1)))
  1620.       ((or (not (char=? (string-ref format:fn-str i) #\0))
  1621.        (<= i format:fn-dot))
  1622.        (set! format:fn-len (+ i 1)))))
  1623.  
  1624. (define (format:fn-zlead)        ; count leading zeros
  1625.   (do ((i 0 (+ i 1)))
  1626.       ((or (= i format:fn-len)
  1627.        (not (char=? (string-ref format:fn-str i) #\0)))
  1628.        (if (= i format:fn-len)        ; found a real zero
  1629.        0
  1630.        i))))
  1631.  
  1632.  
  1633. ;;; some global functions not found in SLIB
  1634.  
  1635. (define (format:string-capitalize-first str) ; "hello" -> "Hello"
  1636.   (let ((cap-str (string-copy str))    ; "hELLO" -> "Hello"
  1637.     (non-first-alpha #f)        ; "*hello" -> "*Hello"
  1638.     (str-len (string-length str)))    ; "hello you" -> "Hello you"
  1639.     (do ((i 0 (+ i 1)))
  1640.     ((= i str-len) cap-str)
  1641.       (let ((c (string-ref str i)))
  1642.     (if (char-alphabetic? c)
  1643.         (if non-first-alpha
  1644.         (string-set! cap-str i (char-downcase c))
  1645.         (begin
  1646.           (set! non-first-alpha #t)
  1647.           (string-set! cap-str i (char-upcase c)))))))))
  1648.  
  1649. (define (format:list-head l k)
  1650.   (if (= k 0)
  1651.       '()
  1652.       (cons (car l) (format:list-head (cdr l) (- k 1)))))
  1653.  
  1654.  
  1655. ;; Aborts the program when a formatting error occures. This is a null
  1656. ;; argument closure to jump to the interpreters toplevel continuation.
  1657.  
  1658. (define format:abort (lambda () (slib:error "error in format")))
  1659.  
  1660. (define format format:format)
  1661.  
  1662. ;; If this is not possible then a continuation is used to recover
  1663. ;; properly from a format error. In this case format returns #f.
  1664.  
  1665. ;(define format:abort
  1666. ;  (lambda () (format:error-continuation #f)))
  1667.  
  1668. ;(define format
  1669. ;  (lambda args                ; wraps format:format with an error
  1670. ;    (call-with-current-continuation    ; continuation
  1671. ;     (lambda (cont)
  1672. ;       (set! format:error-continuation cont)
  1673. ;       (apply format:format args)))))
  1674.  
  1675. ;eof
  1676.